.DEFAULT_GOAL := help

# 获取项目名称（从远程仓库URL中提取）
PROJECT_NAME := $(shell basename -s .git $$(git config --get remote.origin.url))
# 获取当前分支名称
BRANCH_NAME := $(shell git rev-parse --abbrev-ref HEAD)

# 判断分支名是否为 master 并设置最终的 PROJECT_NAME
	ifeq ($(BRANCH_NAME),master)
PROJECT_NAME := $(PROJECT_NAME)
	else
PROJECT_NAME := $(PROJECT_NAME)-$(BRANCH_NAME)
	endif

# 可用参数定义 CURDATE 的值
CURDATE?=$(shell date '+%Y%m%d')
# REGISTRY=hub.ebfcn.net/jkb
REGISTRY=registry.cn-shanghai.aliyuncs.com/haifengat
# 测试环境部署
	ifeq (${MAKECMDGOALS}, test)
server=192.168.52.201
# 生产环境
	else ifeq (${MAKECMDGOALS}, prod)
server=192.168.52.231
	else # 本地
server=192.168.71.65
	endif

build: # 编译 打包镜像
# 修改nginx.conf 中的后端服务地址
	echo '若代码有修改请 先执行 pnpm build !!!'
	docker build . -t ${REGISTRY}/${PROJECT_NAME}:${CURDATE} --no-cache
docker:
	pnpm build
	docker build . -t ${REGISTRY}/${PROJECT_NAME}:${CURDATE} --no-cache
	docker push ${REGISTRY}/${PROJECT_NAME}:${CURDATE}
rm:
	- ssh root@${server} "docker rm -f ${PROJECT_NAME}-${MAKECMDGOALS}"
	- ssh root@${server} "docker pull ${REGISTRY}/${PROJECT_NAME}:${CURDATE}"
webtest webprod:	
	- ssh root@${server} "docker rm -f ${PROJECT_NAME}"
	- ssh root@${server} "docker pull ${REGISTRY}/${PROJECT_NAME}:${CURDATE}"
	ssh root@${server} "docker run -d --name ${PROJECT_NAME} \
	-p 31008:80 \
	--add-host=endpoint:${server} \
	${REGISTRY}/${PROJECT_NAME}:${CURDATE}"
	echo "$(shell date '+%Y/%m/%d %H:%M:%S') ${MAKECMDGOALS} ${REGISTRY}/${PROJECT_NAME}:${CURDATE}" >> deploy.log
local:
	docker rm -f ${PROJECT_NAME}
	docker run -d --name ${PROJECT_NAME} \
	-p 31008:80 \
	--add-host=endpoint:${server} \
	${REGISTRY}/${PROJECT_NAME}:${CURDATE}
	echo "$(shell date '+%Y/%m/%d %H:%M:%S') ${MAKECMDGOALS} ${REGISTRY}/${PROJECT_NAME}:${CURDATE}" >> deploy.log
help:
	@echo '打包镜像('若代码有修改请 先执行 pnpm build !!!'):
	@echo '镜像制作: make build'
	@echo '镜像推送: make docker'
	@echo '远程部署(test/prod):      make prod CURDATE=20210928  旧版 CURDATE=20210823'
	@echo 'make -n 检查语法'
	@echo 'make xxxx CURDATE=yyyymmdd 指定日期(版本)'
